package com.iot.iotplus.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AcceptCallBack implements MqttCallback {

    /**
     * 连接断开
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("【接受消息客户端与mqtt远程服务器连接断开！】");
        AcceptClient.reconnect();
    }

    /**
     * 接收到消息
     * @param topic
     * @param mqttMessage
     * @throws Exception
     */
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        log.info("【接收消息主题】:" + topic);
        log.info("【接收消息Qos】:" + mqttMessage.getQos());
        log.info("【接收消息内容】:"+ new String(mqttMessage.getPayload()));
    }

    /**
     * 消息发送完成
     * @param iMqttDeliveryToken
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        System.out.println("消息发送成功");
    }
}
